<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/yuwanzi.io/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/yuwanzi.io/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/yuwanzi.io/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/yuwanzi.io/images/logo.svg" color="#222">

<link rel="stylesheet" href="/yuwanzi.io/css/main.css">



<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.1/css/all.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"suyuhuan.gitee.io","root":"/yuwanzi.io/","images":"/yuwanzi.io/images","scheme":"Muse","version":"8.2.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Suche...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta name="description" content="Life of a Pixel 2018译文地址 原文地址  This talk is about how Chrome turns web content into pixels. The entire process is called “rendering”.We’ll describe what we mean by content, and what we mean by pixels,">
<meta property="og:type" content="article">
<meta property="og:title" content="Life-of-a-Pixel-2018">
<meta property="og:url" content="https://suyuhuan.gitee.io/yuwanzi.io/2019/08/24/2019-08-24-Life-of-a-Pixel-2018/index.html">
<meta property="og:site_name" content="玉丸子 | Blog">
<meta property="og:description" content="Life of a Pixel 2018译文地址 原文地址  This talk is about how Chrome turns web content into pixels. The entire process is called “rendering”.We’ll describe what we mean by content, and what we mean by pixels,">
<meta property="og:locale">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823213543.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823213714.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823213937.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214046.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214159.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214334.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214537.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214732.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215036.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215155.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215245.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215411.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215532.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215626.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215720.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215812.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215913.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220029.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220106.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220156.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220302.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220351.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220625.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220814.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220907.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221032.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221107.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221124.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221139.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221203.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221218.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221237.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221310.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221326.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221411.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221448.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221509.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221535.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221740.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221936.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222111.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222630.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222733.png">
<meta property="og:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222807.png">
<meta property="article:published_time" content="2019-08-24T09:31:36.000Z">
<meta property="article:modified_time" content="2020-11-07T00:58:17.000Z">
<meta property="article:author" content="玉丸子">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="浏览器">
<meta property="article:tag" content="2019">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823213543.png">


<link rel="canonical" href="https://suyuhuan.gitee.io/yuwanzi.io/2019/08/24/2019-08-24-Life-of-a-Pixel-2018/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh'
  };
</script>
<title>Life-of-a-Pixel-2018 | 玉丸子 | Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/yuwanzi.io/atom.xml" title="玉丸子 | Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Navigationsleiste an/ausschalten" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/yuwanzi.io/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">玉丸子 | Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Inhaltsverzeichnis
        </li>
        <li class="sidebar-nav-overview">
          Übersicht
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Life-of-a-Pixel-2018"><span class="nav-number">1.</span> <span class="nav-text">Life of a Pixel 2018</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">玉丸子</p>
  <div class="site-description" itemprop="description">这里是玉丸子的个人博客,与你一起发现更大的世界。</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/yuwanzi.io/archives">
          <span class="site-state-item-count">68</span>
          <span class="site-state-item-name">Artikel</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/yuwanzi.io/categories/">
        <span class="site-state-item-count">39</span>
        <span class="site-state-item-name">Kategorien</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/yuwanzi.io/tags/">
        <span class="site-state-item-count">46</span>
        <span class="site-state-item-name">schlagwörter</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh">
    <link itemprop="mainEntityOfPage" href="https://suyuhuan.gitee.io/yuwanzi.io/2019/08/24/2019-08-24-Life-of-a-Pixel-2018/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/yuwanzi.io/images/avatar.gif">
      <meta itemprop="name" content="玉丸子">
      <meta itemprop="description" content="这里是玉丸子的个人博客,与你一起发现更大的世界。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="玉丸子 | Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Life-of-a-Pixel-2018
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Veröffentlicht am</span>

      <time title="Erstellt: 2019-08-24 17:31:36" itemprop="dateCreated datePublished" datetime="2019-08-24T17:31:36+08:00">2019-08-24</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Bearbeitet am</span>
        <time title="Geändert am: 2020-11-07 08:58:17" itemprop="dateModified" datetime="2020-11-07T08:58:17+08:00">2020-11-07</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">in</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a>
        </span>
          . 
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/yuwanzi.io/categories/%E5%89%8D%E7%AB%AF/%E6%B5%8F%E8%A7%88%E5%99%A8/" itemprop="url" rel="index"><span itemprop="name">浏览器</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="Life-of-a-Pixel-2018"><a href="#Life-of-a-Pixel-2018" class="headerlink" title="Life of a Pixel 2018"></a>Life of a Pixel 2018</h1><p><a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000016335571">译文地址</a></p>
<p><a target="_blank" rel="noopener" href="http://bit.ly/lifeofapixel">原文地址</a></p>
<blockquote>
<p>This talk is about how Chrome turns web content into pixels. The entire process is called “rendering”.<br>We’ll describe what we mean by content, and what we mean by pixels, and then we’ll explain the magic in between.</p>
</blockquote>
<p>这个演讲主要介绍chrome是如何把web content转成像素点的，整个过程被称为“rendering”。<br>我们首先描述什么叫做content，然后再介绍pixels，然后解释其中的魔法。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823213543.png"></p>
<blockquote>
<p>Chrome’s architecture is constantly evolving. This talk connects high-level concepts (which change slowly) to specific classes (which change frequently).<br>The details are based primarily on what is currently shipping in the canary channel (M69), but a few of the biggest future refactorings are mentioned in passing.</p>
</blockquote>
<p>chrome的架构在不断发展。这个演讲将高级概念（变化缓慢）与特定类（经常变化）联系起来。细节基于canary（M69），一些比较大的未来的重构也会被提到。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823213714.png"></p>
<blockquote>
<p>“Content” is the generic term in Chromium for all of the code inside a webpage or the frontend of a web application.<br>It’s made of text, markup (surrounding the text), styles (defining how markup is rendered), and script (which can modify all of the above dynamically).<br>There are other kinds of content, which we won’t cover here.</p>
</blockquote>
<p>“Content”是最通用的Chromium的术语，指的是网页内部或者是网络应用的前端代码。它由text，markup(环绕于文本),style(决定标记如何渲染)，以及script(能够动态地改变上述提到的内容)。同时也有其他的content，但是我们在这里暂时不做介绍。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823213937.png"></p>
<blockquote>
<p>A real webpage is just thousands of lines of HTML, CSS, and JavaScript delivered in plain text over the network.<br>There’s no notion of compilation or packaging as you might find on other kinds of software platforms - the webpage’s source code is the input to the renderer.</p>
</blockquote>
<p>一个真实的网页由成千行的纯文本的HTML，CSS以及JavaScript通过网络提供。你可能在其他软件平台找不到compilation或者packaging的概念，网页的源代码是renderer的输入。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214046.png"></p>
<blockquote>
<p>Architecturally, the “content” namespace in the Chromium C++ codebase is responsible for everything in the red box.<br>Contrast with tab strip, address bar, navigation buttons, menus, etc. which live outside of “content”.<br>Key to Chrome’s security model: rendering happens in a sandboxed process.</p>
</blockquote>
<p>从架构上说，”content“在Chromium C++ 代码的命名空间负责红框中的所有内容。<br>对比而言，标签条，地址栏，导航按钮，菜单，在”content“之外。<br>Chrome安全模型的关键是，在沙盒进程中进行渲染。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214159.png"></p>
<blockquote>
<p>At the other end of the pipeline we have to get pixels onto the screen using the graphics libraries provided by the underlying operating system.<br>On most platforms today, that’s a standardized API called “OpenGL”. On Windows there’s an extra translation into DirectX. In the future, we may support newer APIs such as Vulkan.<br>These libraries provide low-level graphics primitives like “textures” and “shaders”, and let you do things like “draw a polygon at these coordinates into a buffer of virtual pixels”. But obviously they don’t understand anything about the web or HTML or CSS.</p>
</blockquote>
<p>在管道的另一边，我们要利用操作系统下提供的图形库把像素点放置在屏幕上。现今，在大多数的平台上，有一个标准API叫做”OpenGL”。在Windows上，有额外到DirectX的转换。在将来，我们会支持新的API（类似Vulkan）。<br>这些库提供了低级图形基元（primitive），类似“纹理（texture）”以及“着色器（shader）”，让你能够做类似“在这些坐标处绘制一个多边形到虚拟像素缓冲区”这样的操作。但是显然他们并不理解任何和HTML和CSS相关的事情。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214334.png"></p>
<blockquote>
<p>So the goal of rendering can be stated as: turn HTML / CSS / JavaScript into the right OpenGL calls to display the pixels.<br>But keep in mind a second goal as we describe the pipeline: We also want the right intermediate data structures to update the rendering efficiently after it’s produced, and answer queries about it from script or other parts of the system.</p>
</blockquote>
<p>所以渲染的目标可以描述为：将HTML/CSS/JavaScript转化为正确的OpenGL的调用来展示像素。<br>但是要记住，我们描述这个管道的第二个目标是：我们同样想知道正确的中间数据生成后有效地更新，并且能在脚本或者系统的其他部分回答有关它的查询。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214537.png"></p>
<blockquote>
<p>We break the pipeline into multiple “lifecycle stages”, generating those intermediate outputs.<br>We’ll first describe each stage of a working pipeline, then come back to the notion of efficient updating and introduce some concepts for optimization.</p>
</blockquote>
<p>我们将管道分成多个“生命周期阶段”，产生中间输出。我们首先描述工作流水线的每个阶段，然后回到有效更新的概念，再介绍优化相关的概念。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823214732.png"></p>
<blockquote>
<p>HTML tags impose a semantically meaningful hierarchical structure on the document. For example, a <div> may contain two paragraphs, each with text. So the first step is to parse those tags to build an object model that reflects this structure.</p>
</blockquote>
<p>HTML标签在文档上强加了语义上有意义的层次结构。 例如，&lt;div&gt;可能包含两个段落，每个段落都带有文本。 因此，第一步是解析这些标签以构建反映此结构的对象模型。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215036.png"></p>
<blockquote>
<p>If you’ve taken computer science classes you may recognize this as a “tree”.</p>
</blockquote>
<p>如果你上过计算机科学的课程，那么你就能认出来它是一棵树。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215155.png"></p>
<blockquote>
<p>The DOM serves double duty as both the internal representation of the page, and the API exposed to script for querying or modifying the rendering.<br>The JavaScript engine (V8) exposes DOM web APIs as thin wrappers around the real DOM tree through a system called “bindings”.</p>
</blockquote>
<p>DOM具有双重功能，它既能作为页面的内部展示，也能作为查询和更改呈现的脚本的公开API。Javascript引擎(V8)通过一个系统叫做“bindings”把真实的DOM tree封装成一个轻量的wrapper。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215245.png"></p>
<blockquote>
<p>Having built the DOM tree, the next step is to process the CSS styles.<br>A CSS selector selects a subset of DOM elements that its property declarations should apply to.</p>
</blockquote>
<p>构建了DOM tree以后，下一步是处理CSS styles。一个CSS选择器选择它属性声明了应当应用的DOM元素的子集。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215411.png"></p>
<blockquote>
<p>Style properties are the knobs by which web authors can influence the rendering of DOM elements.<br>There are hundreds of style properties.</p>
</blockquote>
<p>作者可以通过Style属性影响DOM元素的渲染。style属性有几百个。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215532.png"></p>
<blockquote>
<p>Furthermore, it’s not trivial to determine which elements a style rule selects.<br>Some elements may be selected by more than one rule, with conflicting declarations for a particular style property.</p>
</blockquote>
<p>此外，确定样式规则选择哪些元素并非易事。<br>某些元素可能由多个规则选择，并且具有特定样式属性的冲突声明。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215626.png"></p>
<blockquote>
<p>The layout stage runs after the style recalc stage.<br>First, the layout tree is constructed. Then, we walk the layout tree, filling in the geometry data, and processing side effects of the geometry.</p>
</blockquote>
<p>layout阶段在style重计算阶段以后进行。首先，layout tree构建，然后我们遍历这个layout tree，填充几何数据，然后处理几何产生的副作用。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215720.png"></p>
<blockquote>
<p>Today, layout objects contain both inputs and outputs of the layout stage, without a clean separation between them.<br>For example, the LayoutObject acquires ownership of its element’s ComputedStyle object.<br>A new layout system called LayoutNG is expected to simplify the architecture, and make it easier to build new layout algorithms.</p>
</blockquote>
<p>如今，layout对象包括layout阶段的输入和输出,它们之间的区分没有那么清晰。<br>比如，LayoutObject获取它元素的 ComputedStyle 对象的所有权。一个新的layout系统叫做LayoutNG，被期望用来简化架构，构建新的更简单的layout算法。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215812.png"></p>
<blockquote>
<p>Now that we understand the geometry of our layout objects, it’s time to paint them.<br>Paint records paint operations into a list of display items.<br>A paint operation might be something like “draw a rectangle at these coordinates, in this color”.<br>There may be multiple display items for each layout object, corresponding to different parts of its visual appearance, like the background, foreground, outline, etc.<br>This is just a recording that can be played back later. We’ll see why that’s useful in a bit.</p>
</blockquote>
<p>现在我们理解了layout object的几何形状，接下来就是绘制它们。<br>paint将paint操作记录为一列display item。一个paint操作类似于“在这些坐标用这个颜色绘制一个矩形”。对于每个layout object也许有多个display item，对应视觉外观的不同部分，比如背景，前景，轮廓等。~~~</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823215913.png"></p>
<blockquote>
<p>It’s important to paint elements in the right order, so that they stack correctly when they overlap.<br>The order can be controlled by style.</p>
</blockquote>
<p>以正确的顺序paint element非常重要，只有这样才能在元素覆盖的时候正常显示。这个顺序能被style所控制。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220029.png"></p>
<blockquote>
<p>It’s even possible for an element to be partly in front of and partly behind another element.<br>That’s because paint runs in multiple phases, and each paint phase does its own traversal of a subtree.</p>
</blockquote>
<p>一个元素部分在另外一个元素的前面或者后面是可能的。因为paint在不同阶段运行，每次的paint阶段做了自己子树的遍历。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220106.png"></p>
<blockquote>
<p>The paint operations in the display item list are executed by a process called rasterization.<br>Each cell in the resulting bitmap holds values for four color channels.</p>
</blockquote>
<p>在display item列表上进行paint操作被称为rasterization（光栅化）。位图上的每个像素点存着四个颜色通道的值。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220156.png"></p>
<blockquote>
<p>The rastered bitmap is stored in memory, typically GPU memory referenced by an OpenGL texture object.<br>The GPU can also run the commands that produce the bitmap (“accelerated rasterization”).<br>Note that these pixels are not yet on the screen!</p>
</blockquote>
<p>光栅化的位图在内存中存储，通常是被OpenGL纹理对象引用的GPU内存。<br>GPU能够运行产生位图的命令（“加速光栅化”）<br>注意到此时像素并不在屏幕上。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220302.png"></p>
<blockquote>
<p>Rasterization issues OpenGL calls through a library called Skia. Skia provides a layer of abstraction around the hardware, and understands more complex things like paths and Bezier curves.<br>Skia is open-source and maintained by Google. It ships in the Chrome binary but lives in a separate code repository. It’s also used by other products such as the Android OS.<br>Skia’s GPU-accelerated codepath builds its own buffer of drawing operations, which is flushed at the end of the raster task.</p>
</blockquote>
<p>Rasterization通过一个Skia的库产生OpenGL调用，Skia围绕硬件提供了一层抽象，能理解类似路径和贝塞尔曲线等更复杂的事情。<br>Skia开源且由谷歌维护。它附带在chrome二进制文件中，但是属于一个单独的代码库，同时也被其他产品，比如安卓系统使用。<br>Skia的GPU加速代码路径构建了它独立的绘制操作缓冲区，它会在光栅化任务结束的时候刷新。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220351.png"></p>
<blockquote>
<p>Recall that the renderer process is sandboxed, so it can’t make system calls directly.<br>GL calls issued by Skia are actually proxied into a different process using a “command buffer”.<br>The GPU process receives the command buffer and issues the “real” GL calls through a set of function pointers.<br>Besides escaping the renderer sandbox, isolating GL in the GPU process protects us from unstable or insecure graphics drivers.</p>
</blockquote>
<p>回顾之前说的渲染进程在沙盒中，所以并不能直接进行系统调用。<br>Skia发起的GL调用使用“command buffer”被代理给了另一个不同的进程。<br>GPU进程接受command buffer然后通过函数指针发起“真正”的GL调用。<br>逃离沙盒进程，GPU进程中独立的GL将从不稳定和不安全的图形驱动中保护我们。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220625.png"></p>
<blockquote>
<p>Those GL function pointers are initialized by dynamic lookup from the system’s shared OpenGL library - or the ANGLE library on Windows.<br>ANGLE is another library built by Google; its job is to translate OpenGL to DirectX, which is Microsoft’s API for accelerated graphics on Windows.<br>There are also OpenGL drivers for Windows, but historically they have not been very high quality.</p>
</blockquote>
<p>这些GL函数指针通过系统的共享OpenGL库（或Windows上的ANGLE库）进行动态查找来初始化。<br>ANGLE是Google建立的另一个库; 它的工作是将OpenGL转换为DirectX，这是微软在Windows上加速图形的API。<br>Windows也有OpenGL驱动程序，但从历史上看，它们的质量并不高。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220814.png"></p>
<blockquote>
<p>Moving raster to the GPU process will improve performance.<br>It’s also needed to support Vulkan.</p>
</blockquote>
<p>把光栅化移动到GPU进程能够改善性能。它同时需要支持Vulkan</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823220907.png"></p>
<blockquote>
<p>We have now gone all the way from content to pixels in memory.<br>But note that the rendering is not static.<br>Running the full pipeline is expensive</p>
</blockquote>
<p>现在我们已经走完在内存中从content到pixels的全程了。但是渲染本身不是静态的，走完管道全程总是代价昂贵的。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221032.png"></p>
<p>Change is modelled as animation frames.<br>Each frame is a complete rendering of the state of the content at a particular point in time.</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221107.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221124.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221139.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221203.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221218.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221237.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221310.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221326.png"></p>
<blockquote>
<p>Certain style properties cause a layer to be created for a layout object.<br>If a layout object doesn’t have a layer, it paints into the layer of the nearest ancestor that has one.</p>
</blockquote>
<p>特定的样式属性会为layout对象创建layer。如果一个layout对象本身并没有layer，那么它将被绘制到最近的祖先节点上。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221411.png"></p>
<blockquote>
<p>Building the layer tree is a new lifecycle stage on the main thread. Today, this happens before paint, and each layer is painted separately.</p>
</blockquote>
<p>构建layer tree是主线程上的新生命周期阶段。如今，这发生在paint之前，每层都是单独paint的。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221448.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221509.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221535.png"></p>
<blockquote>
<p>After paint is finished, the commit updates a copy of the layer tree on the compositor thread, to match the state of the tree on the main thread.</p>
</blockquote>
<p>paint结束后，commit将更新compositor线程上的layer tree的拷贝，以匹配主线程上tree的状态</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221740.png"></p>
<blockquote>
<p>Recall: raster is the step after paint, which turns paint ops into bitmaps.<br>Layers can be large - rastering the whole layer is expensive, and unnecessary if only part of it is visible.<br>So the compositor thread divides the layer into tiles.<br>Tiles are the unit of raster work. Tiles are rastered with a pool of dedicated raster threads. Tiles are prioritized based on their distance from the viewport.<br>(Not shown: a layer actually has multiple tilings for different resolutions.)</p>
</blockquote>
<p>回想一下：raster在paint之后，它将paint ops转换为位图。<br>layer可能很大 - 整个layer都光栅化的成本很高，如果只有部分可见则不必要全部光栅化。<br>因此，compositor线程将layer划分为tiles。<br>tiles是raster work的基本单位。 专门的raster线程池对tiles进行光栅化。 根据tiles与viewport的距离确定tiles的优先级。<br>（未显示：layer实际上有不同分辨率的多个tilings。）</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823221936.png"></p>
<blockquote>
<p>Once all the tiles are rastered, the compositor thread generates “draw quads”. A quad is like a command to draw a tile in a particular location on the screen, taking into account all the transformations applied by the layer tree. Each quad references the tile’s rastered output in memory (remember, no pixels are on the screen yet).<br>The quads are wrapped up in a compositor frame object which gets submitted to the browser process.</p>
</blockquote>
<p>一旦所有tiles都被光栅化，compositor线程就会生成“draw quads”。 quads类似一个命令，考虑了图层树应用的所有变换在屏幕上的特定位置绘制tile。 每个quads在内存中引用了tile的光栅输出（请记住，屏幕上还没有像素）。<br>quads被包装在一个compositor frame object中，该对象被提交给浏览器进程。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222111.png"></p>
<blockquote>
<p>The compositor thread has two copies of the tree, so that it can raster tiles from a new commit while drawing the previous commit.</p>
</blockquote>
<p>合成器线程有两个树的副本，因此它可以在绘制上一次commit时，对新的commit进行光栅化tiles。</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222630.png"></p>
<blockquote>
<p>The browser process runs a component called the display compositor, inside a service called “viz” (short for visuals).<br>The display compositor aggregates compositor frames submitted from all the renderer processes, along with frames from the browser UI outside of the WebContents. Then it issues the GL calls to draw the quad resources, which go to the GPU process just like the GL calls from the raster workers.<br>On most platforms the display compositor’s output is double-buffered, so the quads draw into a backbuffer, and a “swap” command makes it visible.<br>(On OS X we do something a little different with CoreAnimation.)<br>Finally our pixels are on the screen. :)</p>
</blockquote>
<p>浏览器进程在名为“viz”（visual的简称）的service中运行一个名为display compositor的组件。<br>display compositor聚合从所有renderer进程提交的compositor frame，以及来自WebContents外部的browser UI的frame。 然后它发起GL调用来绘制quad资源，这些资源就像来自raster worker的GL调用一样进入GPU进程。<br>在大多数平台上，display compositor的输出是双缓冲的，因此quads绘制到backbuffer，“swap”命令使其可见。<br>（在OS X上，我们使用CoreAnimation做了一些不同的事情。）<br>最后我们的像素在屏幕上。:)</p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222733.png"></p>
<p><img src="https://picgoimg.oss-cn-beijing.aliyuncs.com/20190823222807.png"></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/yuwanzi.io/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
              <a href="/yuwanzi.io/tags/%E6%B5%8F%E8%A7%88%E5%99%A8/" rel="tag"># 浏览器</a>
              <a href="/yuwanzi.io/tags/2019/" rel="tag"># 2019</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2019/08/10/2019-08-10-How-Blink-Works-%E4%B8%AD%E6%96%87%E8%AF%91%E6%96%87/" rel="prev" title="How Blink Works 中文译文">
                  <i class="fa fa-chevron-left"></i> How Blink Works 中文译文
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/yuwanzi.io/2020/11/07/2020-11-07-%E4%B8%80%E7%A7%92%E6%90%9E%E6%87%82%E4%BB%80%E4%B9%88%E6%98%AFBFC/" rel="next" title="一秒搞懂什么是BFC，告别面试窘境">
                  一秒搞懂什么是BFC，告别面试窘境 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">玉丸子</span>
</div>
  <div class="powered-by">Erstellt mit  <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="//cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/yuwanzi.io/js/utils.js"></script><script src="/yuwanzi.io/js/motion.js"></script><script src="/yuwanzi.io/js/schemes/muse.js"></script><script src="/yuwanzi.io/js/next-boot.js"></script>

  






  





</body>
</html>
